package view;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.Socket;

import commands.AddFileCommand;
import commands.AddFolderCommand;
import commands.DeleteFileCommand;
import commands.DeleteFolderCommand;
import commands.ModifyFileCommand;

import clientRequest.ClientRequest;

/**
 * class MyProtocolGetUserRequest - get from a user its requests about changes 
 * in his dropbox
 */
public class MyProtocolGetUserRequest implements Runnable{
	 
	ObjectInputStream in; PrintWriter out;
	Socket clientSocket;
	 
	/**
	 * constructor
	 * @param in - input stream
	 * @param out - output stream
	 * @param clientSocket - the client socket
	 */
	public MyProtocolGetUserRequest(ObjectInputStream in, PrintWriter out, Socket clientSocket){
		this.in=in; this.out=out;
		this.clientSocket = clientSocket;
	 }
	
	/**
	 * get from a user its requests about changes
	 */
	public void run(){

		//commands
		AddFileCommand addFileCommand = new AddFileCommand();
		DeleteFileCommand deleteFileCommand = new DeleteFileCommand();
		ModifyFileCommand modifyFileCommand = new ModifyFileCommand();
		AddFolderCommand addFolderCommand = new AddFolderCommand();
		DeleteFolderCommand deleteFolderCommand = new DeleteFolderCommand();
		ClientRequest clientRequest;
		
		try {
			
			//first reading
			clientRequest = (ClientRequest) in.readObject();
			clientRequest.printRequest();
			
			//read the user request until the user say "bye"
			while ((clientRequest!=null) && (!clientRequest.getKindRequest().equals("bye")))
			{
				//create file/folder
				if (clientRequest.getKindRequest().equals("ENTRY_CREATE"))
				{					
					if (!clientRequest.isFolder())
						addFileCommand.action(clientRequest);
					else
						addFolderCommand.action(clientRequest);
					
				}
				//delete file/folder
				else if (clientRequest.getKindRequest().equals("ENTRY_DELETE"))
				{					
					deleteFileCommand.action(clientRequest);
					deleteFolderCommand.action(clientRequest);
					
				}
				//modify file
				else if (clientRequest.getKindRequest().equals("ENTRY_MODIFY"))
				{
					
					if (!clientRequest.isFolder())
						modifyFileCommand.action(clientRequest);				
				}
				
				//read the next request		
				clientRequest = (ClientRequest) in.readObject();
				clientRequest.printRequest();
			}
						
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("my pr");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("my pr");
			e.printStackTrace();
		}
		finally {
			
			//close in,out,client socket
			
			if (out!=null)				
				out.close();
			
			try {
				if (in!=null)
					in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				if (clientSocket!=null)
					clientSocket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

}